package jnpf.engine.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.engine.enums.FlowSelectorEnums;
import jnpf.permission.entity.UserEntity;
import jnpf.permission.entity.UserRelationEntity;
import jnpf.permission.service.UserRelationService;
import jnpf.permission.service.UserService;
import jnpf.permission.service.impl.UserRelationServiceImpl;
import jnpf.permission.service.impl.UserServiceImpl;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: duanxu
 * @create: 2025-02-14 14:45
 **/
public class FlowInitiatorUtil {

    private static UserService userService;
    private static UserRelationService userRelationService;

    private static Boolean isInit = false;
    private static final String SPLIT_CHART = "--";

    private static void init() {
        userService = SpringContextHolder.getBean(UserServiceImpl.class);
        userRelationService = SpringContextHolder.getBean(UserRelationServiceImpl.class);
        isInit = true;
    }


    public static List<String> parseInitiator(List<String> initiatorList) {
        // 未初始化依赖bean则初始化
        if (!isInit) {
            init();
        }
        List<String> result = new LinkedList<>();
        initiatorList.forEach(t -> {
            String id = getId(t);
            FlowSelectorEnums selectorEnums = getType(t);
            switch (selectorEnums) {
                case USER:
                    UserEntity user = userService.getById(id);
                    if (ObjectUtil.isNotNull(user)) {
                        result.add(user.getId());
                    }
                    break;
                case DEPARTMENT:
                case GROUP:
                case ROLE:
                case POSITION:
                    metaUserIds(selectorEnums, id, result);
                    break;
                default:
                    break;
            }
        });
        return result;
    }

    private static void metaUserIds(FlowSelectorEnums selectorEnums, String id, List<String> res) {
        if (selectorEnums.getUserRelationType() == null) {
            return;
        }
        List<String> userIds = userRelationService.list(Wrappers.lambdaQuery(UserRelationEntity.class)
                        .select(UserRelationEntity::getUserId)
                        .eq(UserRelationEntity::getObjectType, selectorEnums.getUserRelationType().getType())
                        .eq(UserRelationEntity::getObjectId, id)
                        .groupBy(UserRelationEntity::getUserId))
                .stream()
                .map(UserRelationEntity::getUserId)
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        res.addAll(userIds);
    }

    private static FlowSelectorEnums getType(String s) {
        String[] sp = s.split(SPLIT_CHART);
        String type = sp[1];
        return FlowSelectorEnums.getEnum(type);
    }

    private static String getId(String s) {
        String[] sp = s.split(SPLIT_CHART);
        return sp[0];
    }
}
